﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Intelligent.OTC.Domain.Repositories;
using Intelligent.OTC.Domain.DataModel;
using Intelligent.OTC.Domain;
using Intelligent.OTC.Common;
using System.Data.Entity.Validation;
using Intelligent.OTC.Common.UnitOfWork;

namespace Intelligent.OTC.Business
{
    public class SiteService
    {
        public OTCRepository CommonRep { get; set; }
        public string[] DataBaseInfo;
        public ICacheService CacheSvr { get; set; }
        public List<Sites> GetAllSites()
        {
            //return CacheSvr.GetOrSet<List<Sites>>("Cache_Sites", () =>
            //{
                return CommonRep.GetDbSet<Sites>().Where(o => o.Deal == AppContext.Current.User.Deal).ToList();
            //});
        }

        public IQueryable<Sites> GetSites(string siteCode)
        {
            //return CommonRep.GetQueryable<SysTypeDetail>();

            var Result = from sites in GetAllSites()
                         where sites.LegalEntity==siteCode
                         select sites;

            return Result.AsQueryable();
        }

        public IQueryable<Sites> GetSites()
        {
            //return CommonRep.GetQueryable<SysTypeDetail>();
            //var Result = from sites in GetAllSites()
                         //group sites by sites.SiteCode into rtnSites
                         //select sites;
                         //select new {
                         //    Id = rtnSites.Max(o => o.Id),
                         //    SiteCode = rtnSites.Key,
                         //    SiteNameSys = rtnSites.Max(o => o.SiteNameSys),
                         //    Description = rtnSites.Max(o => o.Description)
                         //};
            
            //var rtn = Result.AsQueryable();
            var Result = CommonRep.GetDbSet<Sites>().Where(o => o.Deal == AppContext.Current.User.Deal);
            return Result.AsQueryable();
        }
    }
}
